(function(){
	var helper = Ccay.Modal = {
		create:function(op){
			return new Ccay.IModal(op)
		},
		//判断是否为实体模型对象
		isModal:function(modal){
			return modal && modal.__name == "Modal";
		},
		//判断是否为必填字段
		isRequiredField:function(field){
			if(field)
			{
				if((field.editorOp && field.editorOp.rule && field.editorOp.rule.required) 
					|| (field.rule && field.rule.required))
					return true;
			}
			return false;
		},
		//初始化ajax设置
		initAjax:function(ajax,op,type){
			if(ajax)
			{
				if(typeof(ajax) == "string")
				{
					return {
						url:ajax,
						success:op.success,
						error:op.error,
						data:op.data,
						type:type
					};
				}
				else
				{
					op.url || (op.url = ajax.url);
					ajax.success && (op.success = $.mergeFn(ajax.success,op.success));
					ajax.error && (op.error = $.mergeFn(ajax.error,op.error));
					op.type || type && (op.type = type);
					return op;
				}
			}
			return null;
		},
		//进行数据请求
		request:function(modal,ajax,op,type,triggerName){
			if(op)
			{
				var ajax;
				if(op.ajax)
					ajax = op.ajax;
				else
				{
					var ajax = this.initAjax(ajax,op,type);
					ajax && op.urlParam && (ajax.url = ajax.url.formatField(op.urlParam,"url"));
				}
				if(ajax)
				{
					var trigger = modal.getTrigger(triggerName);
					if(trigger)
					{
						var fnSuccess = ajax.success;
						ajax.success = function(result){
							modal.fireTrigger(trigger.before,[result]);
							fnSuccess && fnSuccess(result);
							modal.fireTrigger(trigger.after,[result]);
						}
					}
					Ccay.Core.ajax(ajax,op.ui);
				}
			}
		}
	};
	//实体模型
	Ccay.IModal = $.IObject("Modal",{
		init:function(op){
			var modal = this;
			modal._data = {};
			//初始化字段
			if(op.fields)
			{
				this.validator = Ccay.Validator.create();
				if($.isArray(op.fields))
				{
					$.each(op.fields,function(i,field){
						if(!field.alias && op.autoAlias)
							field.alias = field.field.replaceAll('\\.','_');
						modal[field.alias || field.field] = modal.initField(field);
					})
				}
				else
				{
					if(op.initField !== false)
					{
						$.each(op.fields,function(name,field){
							if(!field.alias && op.autoAlias)
								field.alias = field.field.replaceAll('\\.','_');
							modal[field.alias || field.field] = modal.initField(field);
						})
					}
					else
						$.extend(modal,op.fields);
				}
				modal.fields = op.fields;
				delete op.fields;
			}
			$.extend(true,modal,op);
		},
		//遍历field
		each:function(fn){
			var modal = this;
			$.each(modal.fields,function(i,field){
				modal.proxy(fn,field,i);
			});
		},
		//字段初始化方法
		initField:function(field){
			var rule = field.type ? Ccay.UI.Config.typeRule[field.type] : null;
			if(field.rule || rule)
				this.validator.addField(field.field,field.rule = $.extend(true,{},rule,field.rule));
			return Ccay.Control.initField(field);
		},
		getField:function(name){
			if(name){
				name = name.replace(this.modalName + '.','');
				return Ccay.DataHelper.getObj(name,this);
			}
		},
		addField:function(field){
			var fields = this.fields;
			field = this.initField(field);
			if($.isArray(fields))
				fields.push(field);
			else
				fields[field.alias || field.field] = field;
				
			this[field.alias || field.field] = field;
		},
		removeField:function(fieldName){
			var fields = this.fields;
			
			if($.isArray(fields))
			{
				$.each(fields,function(i,field){
					if((field.alias || field.field) == fieldName)
					{
						fields.splice(i,1);
						return false;
					}
				})
			}
			else
				delete fields[fieldName];
				
			delete this[fieldName];
			this.validator.removeField(fieldName);
		},
		//绑定变化的委托
		bindChange:function(type,fn){
			this._ChangeHandler || (this._ChangeHandler = {});
			//任何改变都触发
			if(type == "everyChange")
			{
				this._everyChange = true; 
			}
			//针对视图的触发
			else if(type = "viewChange")
			{
				this._viewChange = true; 
			}
			this._ChangeHandler[type] || (this._ChangeHandler[type] = $.Callbacks());
			this._ChangeHandler[type].add(fn);
		},
		//绑定rest触发器
		bindRest:function(name,fn){
			this.addTrigger(name,fn);
		},
		//设置字段值
		attr:function(name,value,triggerParam,triggerMode){
			var oldValue = Ccay.DataHelper.getObj(name,this._data);
			if(value === undefined)
				return oldValue;
			else
			{
				if(oldValue !== value)
				{
					if(triggerMode != false && this._ChangeHandler)
					{
						//判断是否视图触发
						if(triggerMode != 'view' && this._viewChange)
							this._ChangeHandler.viewChange.fireWith(this,[name,value,oldValue,triggerParam]);
							
						var eChange = this._ChangeHandler[name];
						this._everyChange && this._ChangeHandler.everyChange.fireWith(this,[name,value,oldValue,triggerParam]);
						eChange && eChange.fireWith(this,[name,value,oldValue,triggerParam]);

					}
					Ccay.DataHelper.setObj(name,this._data,value);
					//this._data[name] = value;
				}
			}
			return this;
		},
		//实体赋值
		setData:function(data){
//			var result = this.valid(data);
//			result === true && (this._data = data);
//			return result;
			this._data = data
		},
		//实体取值
		getData:function(copy){
			return copy ? $.extend(true,{},this._data) : this._data;
		},
		valid:function(data){
			return this.validator ? this.validator.valid(data) : true;
		},
		//请求方法
		request:function(name,param,type){
			var modal = this,ajax = modal.rest[name];
			helper.request(modal,ajax,param,type,name);
		},
		//生成主键参数
		buildParam:function(param){
			if(!$.isPlainObject(param))
			{
				var key = param;
				param = {urlParam:{}};
				param.urlParam[this.modalKey] = key;
			}
			else if(!param.url && !param.urlParam && !param.success && param.param)
			{
				param = {urlParam:param};
			}
			
			return param;
		},
		//查找单条数据
		findSingle:function(param){
			this.request("findSingle",this.buildParam(param));
		},
		//查找多条数据
		findList:function(param){
			this.request("findList",param);
		},
		//判断是否为必须字段
		isRequired:function(field){
			if(typeof(field) == "string")
				field = this.fields[field];
			return helper.isRequiredField(field);
		},
		//是否新增
		isNew:function(){
			return !this.getKey();
		},
		//获取主键值
		getKey:function(){
			return this.attr(this.modalKey);
		},
		//创建方法
		createSingle:function(param){
			param || (param);
			param.data = this.getData();
			this.request("createSingle",param,"POST");
		},
		//更新方法
		updateSingle:function(param){
			param || (param = {});
			param.data = this.getData();
			this.request("updateSingle",param,"PUT");
		},
		//保存方法
		saveSingle:function(param){
			this.isNew() ? this.createSingle(param) : this.updateSingle(param);
		},
		//批量处理方法
		saveBatch:function(param){
			this.request("saveBatch",param,"PUT");
		},
		//删除方法
		deleteSingle:function(param){
			this.request("deleteSingle",this.buildParam(param),"DELETE");
		}
	},null,true);
	
})();